<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
      <el-form-item label="讲师名称">
        <el-input v-model="eduTeacher.name" />
      </el-form-item>
      <el-form-item label="入驻时间">
        <el-date-picker v-model="eduTeacher.joinDate" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="eduTeacher.sort" :min="0"/>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="eduTeacher.level">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
            -->
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="eduTeacher.career"/>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="eduTeacher.intro" :rows="5" type="textarea"/>
      </el-form-item>

      <!-- 讲师头像 -->
      <el-form-item label="讲师头像">
        <pan-thumb :image="eduTeacher.avatar"/>
        <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像</el-button>

        <image-cropper
          v-show="imagecropperShow"
          :width="200"
          :height="200"
          :key="imagecropperKey"
          :url="BASE_API+'/oss-service/oss'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"/>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import teacherApi from '@/api/teacher'
  import ImageCropper from '@/components/ImageCropper'
  import PanThumb from '@/components/PanThumb'

  export default {
    components:{ImageCropper,PanThumb},
    data() {
      return {
        // 讲师对象
        eduTeacher: {},
        saveBtnDisabled: false, // 默认按钮可用
        imagecropperShow: false,
        imagecropperKey: 0,
        BASE_API:process.env.BASE_API
      }
    },

    created() {
      this.init()
    },

    // 路由切换
    watch:{
      $route(to, from){
        this.init()
      }
    },

    methods: {
      close(){
        this.imagecropperShow=false
      },

      cropSuccess(data){
        this.imagecropperShow=false
        this.eduTeacher.avatar = data.url
        this.imagecropperKey += 1
      },

      init(){
        if (this.$route.params.id) {
          this.fetchDataById(this.$route.params.id)
        }
        else{
          this.eduTeacher = {
            sort: 0,
            level: 1,
            avatar: '',
            joinDate: new Date(),
            avatar: "https://rice-college.oss-cn-shenzhen.aliyuncs.com/2020/08/07/89b8b90598be48e1ba3efaeab46b3b96default.jpg"
          }
        }
      },

      saveOrUpdate() {
        console.log(this.eduTeacher)
        this.saveBtnDisabled = true
        if (!this.eduTeacher.id) {
          this.saveData()
        } else {
          this.updateData()
        }
      },

      // 数据保存
      saveData() {
        teacherApi.save(this.eduTeacher).then(response => {
          // 弹出成功提示
          this.$message({
            message: response.message,
            type: 'success'
          })
          this.$router.push({ path: '/teacher/list' })
        })
      },

      // 数据更新
      updateData() {
        teacherApi.updateById(this.eduTeacher).then(response => {
          // 弹出成功提示
          this.$message({
            message: response.message,
            type: 'success'
          })
          this.$router.push({ path: '/teacher/list' })
        })
      },

      // 根据id查询数据
      fetchDataById(id) {
        teacherApi.getById(id).then(response => {
          this.eduTeacher = response.data.eduTeacher
        })
      }
    }
  }
</script>
